import json
import os

CONFIG_FILE = "config.json"

class Config:
    def __init__(self):
        self.data = self.load_config()
        self.subscriptions_file = self.data["subscriptions_file"]

    def load_config(self):
        if not os.path.exists(CONFIG_FILE):
            default_config = {
                "subscriptions_file": "subscriptions.json",
                "database_url": "sqlite:///gitee_sentinel.db",
                "notify_email": "notify@example.com"
            }
            with open(CONFIG_FILE, "w") as file:
                json.dump(default_config, file, indent=4)
            return default_config

        with open(CONFIG_FILE, "r") as file:
            return json.load(file)

    def load_subscriptions(self):
        if not os.path.exists(self.subscriptions_file):
            with open(self.subscriptions_file, "w") as file:
                json.dump({"subscriptions": []}, file, indent=4)
            return []

        with open(self.subscriptions_file, "r") as file:
            return json.load(file)["subscriptions"]

    def save_subscriptions(self, subscriptions):
        with open(self.subscriptions_file, "w") as file:
            json.dump({"subscriptions": subscriptions}, file, indent=4)
    
    def get_gitee_token(self):
        """从配置文件中获取 Gitee Token"""
        return self.data.get("gitee_token", "")
    
    def get_ollama(self):
        """从配置文件中获取 Gitee Token"""
        return self.data.get("ollama_host", "http://localhost:11434")